package com.itheima.controller.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.ums.UmsResource;
import com.itheima.pojo.ums.UmsResourceCategory;
import com.itheima.service.ums.UmsResourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/resource")
public class UmsResourceController {
    @DubboReference
    private UmsResourceService umsResourceService;

    /**
     * 添加后台资源
     *
     * @param umsResource
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody UmsResource umsResource) {
        umsResourceService.create(umsResource);
        return R.success("success");
    }

    /**
     * 根据ID删除后台资源
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        umsResourceService.delete(id);
        return R.success("success");
    }

    /**
     * 查询所有后台资源
     *
     * @return
     */
    @GetMapping("/listAll")
    public R listAll() {
        List<UmsResource> listAll = umsResourceService.listAll();
        return R.success(listAll);
    }

    /**
     * 修改后台资源
     *
     * @param umsResource
     * @return
     */
    @PostMapping("/update/{id}")
    public R update(@RequestBody UmsResource umsResource) {
        boolean res = umsResourceService.updateResource(umsResource);
        return res == true ? R.success() : R.error("此id不存在！");
    }

    /**
     * 根据ID获取资源详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getDetails(@PathVariable Integer id) {
        UmsResource umsResource = umsResourceService.selectById(id);
        return R.success(umsResource);
    }

    /**
     * 分页模糊查询后台资源
     *
     * @param categoryId
     * @param nameKeyword
     * @param urlKeyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R list(Integer categoryId,String nameKeyword, String urlKeyword, Integer pageNum, Integer pageSize) {
        PageInfo umsResourceList = umsResourceService.likeAll(categoryId, nameKeyword, urlKeyword, pageNum, pageSize);
        return R.success(umsResourceList);
    }

}
